﻿using Microsoft.EntityFrameworkCore;
using OL.FrameCore.Application.Person.Dto;
using OL.FrameCore.Domain.Entity;
using OL.FrameCore.Infrastructure.UnitOfWork;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using OL.FrameCore.Infrastructure.Security;

namespace OL.FrameCore.Application.Person
{
    public class PersonService: IPersonService
    {
        IUnitOfWork _unitOfwork;
        public PersonService(IUnitOfWork unitOfwork)
        {
            _unitOfwork = unitOfwork;
        }


        /// <summary>
        /// 更新用户资料
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Result UpdatePersonInfo(UpdatePersonInfoRequest request)
        {
            var repositoryUser = _unitOfwork.GetRepository<SysUser>();
            var model = repositoryUser.Table.SingleOrDefault(p => p.Id == request.UserId);
            if (model == null)
            {
                return new FailResult("找不到用户id:" + request.UserId);
            }
            model.TrueName = request.TrueName;
            model.Sex = request.Sex;
            model.Phone = request.Phone;
            model.Email = request.Email;
            model.UpdateId = request.UserId;
            model.UpdateTime = DateTime.Now;
            repositoryUser.Update(model);

            //日志
            var repositoryUserOperateLog = _unitOfwork.GetRepository<SysUserOperateLog>();
            repositoryUserOperateLog.Insert(new SysUserOperateLog
            {
                OperateId = request.UserId,
                OperateType = 1,
                UserId = request.UserId,
                Remark = "修改用户信息",
                IP = "",
                CreateTime = DateTime.Now
            });

            return new SuccessResult();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Result UpdatePassword(UpdatePasswordRequest request)
        {
            //md5加密
            request.OldPassword = MD5Utility.MD5(request.OldPassword);
            request.NewPassword = MD5Utility.MD5(request.NewPassword);
            request.RepeatPassword = MD5Utility.MD5(request.RepeatPassword);

            if (request.NewPassword != request.RepeatPassword)
            {
                return new FailResult("两次新密码不一致");
            }

            var repositoryUser = _unitOfwork.GetRepository<SysUser>();
            var user = repositoryUser.Table.SingleOrDefault(p=>p.Id==request.UserId);
            if (user == null)
            {
                return new FailResult("找不到用户id:" + request.UserId);
            }
            else if (request.OldPassword != user.Password)
            {
                return new FailResult("旧密码不正确");
            }

            user.Password = request.NewPassword;
            user.UpdateId = request.UserId;
            user.UpdateTime = DateTime.Now;
            _unitOfwork.Save();

            //日志
            var repositoryUserOperateLog = _unitOfwork.GetRepository<SysUserOperateLog>();
            repositoryUserOperateLog.Insert(new SysUserOperateLog
            {
                OperateId = request.UserId,
                OperateType = 1,
                UserId = request.UserId,
                Remark = "修登录密码",
                IP = "",
                CreateTime = DateTime.Now
            });

            return new SuccessResult();
        }
        /// <summary>
        /// 获取登录记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PageData<GetLoginRecordListResponse> GetLoginRecordList(GetLoginRecordListRequest request)
        {
            var repositoryUserLoginLog = _unitOfwork.GetRepository<SysUserLoginLog>();
            var query = from a in repositoryUserLoginLog.TableNoTracking
                        where a.UserId== request.UserId
                        orderby a.LoginTime descending
                        select new GetLoginRecordListResponse
                        {
                            Id = a.Id,
                            UserName = a.UserName,
                            UserId = a.UserId,
                            IP = a.IP,
                            UserAgent = a.UserAgent,
                            LoginStatus = a.LoginStatus,
                            LoginTime = a.LoginTime
                        };

            return repositoryUserLoginLog.PagingAsync<GetLoginRecordListResponse>(query, request.PageIndex, request.PageSize).Result;
        }
    }
}
